"""
新浪军事
https://mil.news.sina.com.cn/roll/index.d.html?cid=57919
"""
from AutoOperation.settings import AutoOperationExecutorManager
from conversion.textToImages.baidu import getImages
from bs4 import BeautifulSoup
from tools.loggingConfig import logger
from tools.publicConf import BaseModel, CollectorExecutor, ProducerExecutor
from tools.tool import imageBytesListToBase64List


class MilitarySinaModel(BaseModel):
    """
    注册MilitarySina素材
    数据库model
    """
    class Meta:
        db_table = "MilitarySina"


class MilitarySinaCollectorExecutor(CollectorExecutor):
    """
    数据采集
    sina素材任务的执行器
    """

    def __init__(self):
        self.maxThreadSize = 5
        super(self.__class__, self).__init__(maxThreadSize=self.maxThreadSize, databaseModel=MilitarySinaModel)

    def executorEvent(self):
        """
        执行器任务逻辑
        """

        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
        }
        for page in range(1, 26):
            try:
                url = f"https://mil.news.sina.com.cn/roll/index.d.html?cid=57919&page={page}"
                response = self.request().get(url=url, headers=headers)
                response.encoding = "utf-8"
                bs = BeautifulSoup(response.text, "html.parser")
                aEles = bs.find("div", class_="fixList").find_all("a")
                militaryUrls = [aEle["href"] for aEle in aEles]
                for militaryUrl in militaryUrls:
                    self.get(self.getMilitaryContent, url=militaryUrl, headers=headers)
            except Exception as e:
                logger.error(f"{self.executorName} 采集军事素材urls出错 {e}")

    def getMilitaryContent(self, response):
        """
        获取响应下的
        1.标题
        2.时间
        3.内容
        """
        try:
            response.encoding = "utf-8"
            bs = BeautifulSoup(response.text, "html.parser")
            title = bs.find("h1", class_="main-title").text

            # 判断当前素材是否存在
            with self.modelLock:
                is_exists = self.model.objects.filter(title=title).exists()
                if is_exists:
                    return

            date = bs.find("span", class_="date").text
            year = date.split("年")[0]
            month = date.split("年")[1].split("月")[0]
            day = date.split("年")[1].split("月")[1].split("日")[0]
            time = date.split(" ")[1]
            date = year + "-" + month + "-" + day + " " + time
            article = bs.find("div", id="article").text
            imagesBase64 = imageBytesListToBase64List(getImages(word=title, imgNum=10))
            self.saveDB(title=title, date=date, content=article, imagesBase64=imagesBase64)
        except Exception as e:
            logger.error(f"{self.executorName} 解析军事素材出错 {e}")


class MilitarySinaProducerExecutor(ProducerExecutor):
    """
    成品制作
    sina素材任务的执行器
    """

    def __init__(self):
        self.maxThreadSize = 5
        super(self.__class__, self).__init__(maxThreadSize=self.maxThreadSize, databaseModel=MilitarySinaModel)


AutoOperationExecutorManager.registerExecutor(ExecutorClass=MilitarySinaCollectorExecutor)
AutoOperationExecutorManager.registerExecutor(ExecutorClass=MilitarySinaProducerExecutor)
